setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load topic model
load('models/stm50k.Rdata')

### prepare tasks
newMass <- combMass(stm50k)
WIstm50k <- validateTopic(type = "WI", n = 500, vocab = newMass[[1]], beta = newMass[[2]], thres = 20)
goldWI <- read.csv("goldstandard/goldWI.csv", stringsAsFactors = FALSE)
WIstm50k <- mixGold(tasks = WIstm50k, golds = goldWI)
WIstm50k <- recordTasks(type = "WI", tasks = WIstm50k, path = "topicrecords/WIstm50k.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# WIstm50kids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "WI",
#                          tasksrecord = WIstm50k, HITidspath = "HITids/WIstm50kids1.Rdata")
# ### extend HITs if needed
# for(i in WIstm50kids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*48)
# }
# 
# load("HITids/WIstm50kids1.Rdata")
# WIstm50kresults1 <- getResults(batch_id = "WIstm50k1", hit_ids = HITids, retry = FALSE)
# write.csv(WIstm50kresults1, "../../1_replication/1_topicvalidation/topicresults/WIstm50kresults1.csv", row.names = FALSE)
# evalResults(results = WIstm50kresults1, key = WIstm50k, type = "WI")
# 
# ### send tasks (second trial)
# WIstm50kids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "WI",
#                          tasksrecord = WIstm50k, HITidspath = "HITids/WIstm50kids2.Rdata")
# ### extend HITs if needed
# for(i in WIstm50kids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*48)
# }
# 
# load("HITids/WIstm50kids2.Rdata")
# WIstm50kresults2 <- getResults(batch_id = "WIstm50k2", hit_ids = HITids, retry = FALSE)
# write.csv(WIstm50kresults2, "../../1_replication/1_topicvalidation/topicresults/WIstm50kresults2.csv", row.names = FALSE)
# evalResults(results = WIstm50kresults2, key = WIstm50k, type = "WI")
